博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
01_8_session
阅读量:4520 次
发布时间:2019-06-08

本文共 3381 字,大约阅读时间需要 11 分钟。

01_8_session

1. session总结

1.1服务器的一块内存(key-value)

1.2和客户端窗口对应(子窗口)(独一无二)

1.3客户端和服务器有对应的SessionID

1.4客户端服务器端发送SessionID的时候两种方式

  1. cookie(内存cookie)
  2. rewrite URL

1.5浏览器禁掉cookie,就不能使用session(使用cookie实现session

1.6如果想安全的使用session(不论客户端是否禁止cookie),只能使用URL重写(大大增加编程负担),很多网站要求客户端打开cookie

2.例子

2.1ShowSession.java

public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { response.setContentType("text/html; charset=utf-8");PrintWriter out = response.getWriter();String title = "Session Tracking Example";HttpSession session = request.getSession(true);String heading;Integer accessCount = (Integer) session.getAttribute("accessCount");System.out.println(accessCount);if (accessCount == null) {accessCount = new Integer(0);heading = "Welcom, Newcomer";System.out.println(accessCount);} else {heading = "Welcome Back";accessCount = new Integer(accessCount.intValue() + 1);}session.setAttribute("accessCount", accessCount);/*Integer access = (Integer) session.getAttribute("accessCount");System.out.println(access);*/out.println("");out.println("");out.println("  Session追踪");out.println("  ");out.print("

" + heading + "

");out.print("

Information on Your Session:

");out.println("
Info Type Value
Creation Time " + new Date(session.getCreationTime())+ "
Time of Last Access " + new Date(session.getLastAccessedTime()) + "
Number of Previous Accesses " + accessCount+ "
");out.println(" ");out.println("");out.flush();out.close();}

 

 

2.2SessionInfoServlet.java

public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { response.setContentType("text/html; charset=utf-8");PrintWriter out = response.getWriter();HttpSession session = request.getSession(true); out.println("");out.println("");out.println("  Session Info Servlet");out.println("  ");out.print("

Session Information

");out.print("New Session:" + session.isNew());out.println("
Session ID:" + session.getId());out.println("
Session Creation Time:" + new Date(session.getCreationTime()));out.println("
Session Last Accessed Time:" + new Date(session.getLastAccessedTime()));out.println("

Request Information

");out.println("Session ID from Request:" + request.getRequestedSessionId());out.println("
Session ID Via Cookie:" + request.isRequestedSessionIdFromCookie());out.println("
Session ID Via rewritten URL:" + request.isRequestedSessionIdFromURL());out.println("
Valid Sesion ID:" + request.isRequestedSessionIdValid());out.println(" ");out.println("");out.flush();out.close();}

 

 

2.3URLSession.java

public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { response.setContentType("text/html; charset=utf-8");PrintWriter out = response.getWriter();HttpSession session = request.getSession(true);out.println("");out.println("");out.println("  Session 追踪");out.println("  ");out.print("session id:" + session.getId() + "
");out.print("from url:" + request.isRequestedSessionIdFromUrl() + "
");out.print("from cookie:" + request.isRequestedSessionIdFromCookie() + "
");out.println(" test
");out.println(" test
");out.println(" ");out.println("");out.flush();out.close();}

 

转载于:https://www.cnblogs.com/flyback/p/8721669.html

你可能感兴趣的文章
爬虫—使用Requests
查看>>
scrollIntoView()窗口滚动
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>
使用ansible远程管理集群
查看>>
读jQuery源码释疑笔记3
查看>>
手把手教你jmeter压测--适合入门
查看>>
Sequelize+MySQL存储emoji表情
查看>>
RabbitMQ学习之Publish/Subscribe(3)
查看>>
[SCOI2010]生成字符串
查看>>
JLOI2015 城池攻占
查看>>
在 Azure 虚拟机上快速搭建 MongoDB 集群
查看>>
跑步运动软件调研
查看>>
搭建ntp时间服务器 ntp - (Network Time Protocol)
查看>>
35. Search Insert Position
查看>>
awk使用
查看>>
ASP.NET Razor 视图引擎编程参考
查看>>
Vue 基础篇
查看>>
malloc_free_new_delete
查看>>
Python中的open和codecs.open
查看>>
开发Servlet的方法(2)
查看>>